package UML_ER.diagram.part;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.Tool;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gmf.runtime.diagram.ui.l10n.SharedImages;
import org.eclipse.gmf.runtime.diagram.ui.tools.UnspecifiedTypeConnectionTool;
import org.eclipse.gmf.runtime.diagram.ui.tools.UnspecifiedTypeCreationTool;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import UML_ER.diagram.providers.UML_ERElementTypes;

/**
 * @generated
 */
public class UML_ERPaletteFactory {

	/**
	 * @generated
	 */
	public void fillPalette(PaletteRoot paletteRoot) {
		cleanStandardTools(paletteRoot);
		paletteRoot.add(createDefault1Group());
		paletteRoot.add(createERtypes2Group());
	}

	/**
	 * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=159289
	 * @generated
	 */
	private void cleanStandardTools(PaletteRoot paletteRoot) {
		for (Iterator it = paletteRoot.getChildren().iterator(); it.hasNext();) {
			PaletteEntry entry = (PaletteEntry) it.next();
			if (!"standardGroup".equals(entry.getId())) { //$NON-NLS-1$
				continue;
			}
			for (Iterator it2 = ((PaletteContainer) entry).getChildren()
					.iterator(); it2.hasNext();) {
				PaletteEntry entry2 = (PaletteEntry) it2.next();
				if ("zoomTool".equals(entry2.getId())) { //$NON-NLS-1$
					it2.remove();
				} else if ("noteStack".equals(entry2.getId())) { //$NON-NLS-1$
					it2.remove();
				} else if ("selectionTool".equals(entry2.getId())) { //$NON-NLS-1$
					it2.remove();
				}
				if (paletteRoot.getDefaultEntry() == entry2) {
					paletteRoot.setDefaultEntry(null);
				}
			}
		}
	}

	/**
	 * Creates "Default" palette tool group
	 * 
	 */
	private PaletteContainer createDefault1Group() {
		PaletteGroup paletteContainer = new PaletteGroup(
				Messages.Default1Group_title);
		paletteContainer.setId("createDefault1Group"); //$NON-NLS-1$
		paletteContainer.setDescription(Messages.Default1Group_desc);
		paletteContainer.add(createSelecttoolTool());
		paletteContainer.add(createNoteTool());
		return paletteContainer;
	}

	/**
	 * Creates "ER types" palette tool group
	 * 
	 */
	private PaletteContainer createERtypes2Group() {
		PaletteDrawer paletteContainer = new PaletteDrawer(
				Messages.ERtypes2Group_title);
		paletteContainer.setId("createERtypes2Group"); //$NON-NLS-1$
		paletteContainer.add(createEntity1CreationTool());
		paletteContainer.add(createAttribute7CreationTool());
		paletteContainer.add(createRelationship3CreationTool());
		paletteContainer.add(createGeneralization2CreationTool());
		paletteContainer.add(createAggregation5CreationTool());
		paletteContainer.add(createComposition6CreationTool());
		paletteContainer.add(createComplexRelationship4CreationTool());
		//paletteContainer.add(createRelationshipAttribute8CreationTool());
		//paletteContainer.add(createRelationAttributeName9CreationTool());
		//paletteContainer.add(createRelationAttributeLine10CreationTool());
		//paletteContainer
		//		.add(createNonBinaryRelationshipConnection11CreationTool());
		return paletteContainer;
	}

	/**
	 * @generated
	 */
	private ToolEntry createSelecttoolTool() {
		PanningSelectionToolEntry entry = new PanningSelectionToolEntry();
		entry.setId("createSelecttoolTool"); //$NON-NLS-1$
		return entry;
	}
	

	private ToolEntry createNoteTool() {

		IElementType NOTE = ElementTypeRegistry.getInstance().getType(
				"org.eclipse.gmf.runtime.diagram.ui.presentation.note");
		NodeToolEntry entry = new NodeToolEntry("Note", "Create Note",
				Collections.singletonList(NOTE));
		entry.setId("createNoteTool"); //$NON-NLS-1$

		//PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(SharedImages.DESC_NOTE);

		entry.setSmallIcon(SharedImages.DESC_NOTE);
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createEntity1CreationTool() {
		NodeToolEntry entry = new NodeToolEntry(
				Messages.Entity1CreationTool_title,
				Messages.Entity1CreationTool_desc,
				Collections.singletonList(UML_ERElementTypes.Entity_2003));
		entry.setId("createEntity1CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.Entity_2003));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createGeneralization2CreationTool() {
		LinkToolEntry entry = new LinkToolEntry(
				Messages.Generalization2CreationTool_title,
				Messages.Generalization2CreationTool_desc,
				Collections
						.singletonList(UML_ERElementTypes.Generalization_4011));
		entry.setId("createGeneralization2CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.Generalization_4011));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createRelationship3CreationTool() {
		LinkToolEntry entry = new LinkToolEntry(
				Messages.Relationship3CreationTool_title,
				Messages.Relationship3CreationTool_desc,
				Collections
						.singletonList(UML_ERElementTypes.BinaryRelationship_4014));
		entry.setId("createRelationship3CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.BinaryRelationship_4014));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createComplexRelationship4CreationTool() {
		NodeToolEntry entry = new NodeToolEntry(
				Messages.ComplexRelationship4CreationTool_title,
				Messages.ComplexRelationship4CreationTool_desc,
				Collections.singletonList(UML_ERElementTypes.Relationship_2009));
		entry.setId("createComplexRelationship4CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.Relationship_2009));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createAggregation5CreationTool() {
		LinkToolEntry entry = new LinkToolEntry(
				Messages.Aggregation5CreationTool_title,
				Messages.Aggregation5CreationTool_desc,
				Collections.singletonList(UML_ERElementTypes.Aggregation_4015));
		entry.setId("createAggregation5CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.Aggregation_4015));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createComposition6CreationTool() {
		LinkToolEntry entry = new LinkToolEntry(
				Messages.Composition6CreationTool_title,
				Messages.Composition6CreationTool_desc,
				Collections.singletonList(UML_ERElementTypes.Composition_4025));
		entry.setId("createComposition6CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.Composition_4025));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createAttribute7CreationTool() {
		ArrayList<IElementType> types = new ArrayList<IElementType>(2);
		types.add(UML_ERElementTypes.Attribute_3001);
		types.add(UML_ERElementTypes.Attribute_3003);
		NodeToolEntry entry = new NodeToolEntry(
				Messages.Attribute7CreationTool_title, null, types);
		entry.setId("createAttribute7CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.Attribute_3001));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createRelationshipAttribute8CreationTool() {
		NodeToolEntry entry = new NodeToolEntry(
				Messages.RelationshipAttribute8CreationTool_title,
				Messages.RelationshipAttribute8CreationTool_desc,
				Collections
						.singletonList(UML_ERElementTypes.BinaryRelationship_2008));
		entry.setId("createRelationshipAttribute8CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.BinaryRelationship_2008));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createRelationAttributeName9CreationTool() {
		ToolEntry entry = new ToolEntry(
				Messages.RelationAttributeName9CreationTool_title,
				Messages.RelationAttributeName9CreationTool_desc, null, null) {
		};
		entry.setId("createRelationAttributeName9CreationTool"); //$NON-NLS-1$
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createRelationAttributeLine10CreationTool() {
		ToolEntry entry = new ToolEntry(
				Messages.RelationAttributeLine10CreationTool_title,
				Messages.RelationAttributeLine10CreationTool_desc, null, null) {
		};
		entry.setId("createRelationAttributeLine10CreationTool"); //$NON-NLS-1$
		return entry;
	}

	/**
	 * @generated
	 */
	private ToolEntry createNonBinaryRelationshipConnection11CreationTool() {
		LinkToolEntry entry = new LinkToolEntry(
				Messages.NonBinaryRelationshipConnection11CreationTool_title,
				Messages.NonBinaryRelationshipConnection11CreationTool_desc,
				Collections.singletonList(UML_ERElementTypes.Role_4024));
		entry.setId("createNonBinaryRelationshipConnection11CreationTool"); //$NON-NLS-1$
		entry.setSmallIcon(UML_ERElementTypes
				.getImageDescriptor(UML_ERElementTypes.Role_4024));
		entry.setLargeIcon(entry.getSmallIcon());
		return entry;
	}

	/**
	 * @generated
	 */
	private static class NodeToolEntry extends ToolEntry {

		/**
		 * @generated
		 */
		private final List<IElementType> elementTypes;

		/**
		 * @generated
		 */
		private NodeToolEntry(String title, String description,
				List<IElementType> elementTypes) {
			super(title, description, null, null);
			this.elementTypes = elementTypes;
		}

		/**
		 * @generated
		 */
		public Tool createTool() {
			Tool tool = new UnspecifiedTypeCreationTool(elementTypes);
			tool.setProperties(getToolProperties());
			return tool;
		}
	}

	/**
	 * @generated
	 */
	private static class LinkToolEntry extends ToolEntry {

		/**
		 * @generated
		 */
		private final List<IElementType> relationshipTypes;

		/**
		 * @generated
		 */
		private LinkToolEntry(String title, String description,
				List<IElementType> relationshipTypes) {
			super(title, description, null, null);
			this.relationshipTypes = relationshipTypes;
		}

		/**
		 * @generated
		 */
		public Tool createTool() {
			Tool tool = new UnspecifiedTypeConnectionTool(relationshipTypes);
			tool.setProperties(getToolProperties());
			return tool;
		}
	}
}
